// ==UserScript==
// @name         添加浏览记录到留言板
// @description  ...
// @namespace    http://tampermonkey.net/
// @version      2024-05-22
// @match        http://*/*
// @match        https://*/*
// @icon         http://wuweijun.fun/favicon.ico
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
  'use strict';

  let tttimg ;
  let isSend = false;

  function start(){
      // if(localStorage.getItem('href')==location.href && isSend ) return;
      if(isSend ) return;
      if(location.host.includes('wuweijun.fun')) return;
      let senData = { centent:`[${document.title}](${location.href})`, vavlidity: vavlidityFn(), createDate: new Date().getTime() }
      GM_xmlhttpRequest({
          method: "POST",
          url:'http://liuyanban.wuweijun.fun:999/setData',
          headers: {
              "Content-Type": "application/json;charset=utf-8"
          },
          data:JSON.stringify(senData),
          onload: function (res) {
              if (res.status === 200) {
                  let data = res.responseText;
                  console.log(data)
                  isSend=true;
                  localStorage.setItem('href', location.href)
              } else {
                  console.log(res)
                  alert('添加浏览记录到留言板-失败')
              }
          },
          onerror: function (err) {
              alert('添加浏览记录到留言板-异常!!')
              clearInterval(tttimg);
              console.log(err)
          }
      });

  }

  // 计算有效期时间，默认有效期时长为30天
  function vavlidityFn() {
      return new Date().getTime() + 1000 * 60 * 60 * 24 * 30;
  }

  tttimg = setInterval(() => { start(); }, 1000 * 60 * 5);


  // Your code here...
})();